// pages/detail/detail.js

var api = require('../../utils/api.js');
import { showModal } from '../../utils/Common.js';
import { redirectToTab, redirectToLogin } from '../../utils/Logic.js';
import net from '../../utils/Request.js';

const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        itemData: {},
        imgUrls: [],
        interval: 2000,
        duration: 1000,
        menuId: null,
        remark: [],
        userStatus: 0,
        userRemark: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        this.setData({
            menuId: options.menuId
        });
        this.fetchData();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.fetchData();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    fetchData: function () {
        var that = this;
        wx.showNavigationBarLoading();
        net.post(api.baseUrl + 'api/getMenuDetail', {
            ...app.globalData.access_token,
            'menuId': that.data.menuId
        }).then(res => res.data).then(res => {
            console.log(res);
            if (res.status === 20000) {
                var rms = [];
                var userRemark = '';
                if (res.data.remark != null) {
                    rms = res.data.remark.split(',');
                    if (rms.length > 0) {
                        userRemark = rms[0];
                    }
                }
                console.log(rms);
                var images = [res.data.avatarUrl];
                that.setData({
                    itemData: res.data,
                    remark: rms,
                    imgUrls: images,
                    userRemark: userRemark
                });
            }
        }).catch(e => {
            redirectToLogin();
            console.error('getMenuDetail ', e);
        }).then(() => {
            wx.hideNavigationBarLoading();
            wx.stopPullDownRefresh();
        });
    },

    checkOrderExits: function (e) {
        var that = this;

        wx.showNavigationBarLoading();

        net.post(api.baseUrl + 'api/checkUserTodayIsOrder', {
            ...app.globalData.access_token
        }).then(res => res.data).then(res => {
            console.log('order success', res);
            that.handleOrderResult(res);
        }).catch(e => {
            console.error('order fail ...', e);
        }).then(() => {
            wx.hideNavigationBarLoading();
        });
    },

    handleOrderResult: function (res) {
        console.log(res);
        if (res.status === 40010) {
            showModal('提示', res.message);
        } else if (res.status === 20001) {
            var that = this;
            showModal('提示', '订单已经存在,请确定更新?', () => {
                console.log('update');
                that.gotoOrder();
            });
        } else {
            this.gotoOrder();
        }
    },

    gotoOrder: function () {
        wx.showLoading({
            title: '下单中....',
            mask: true
        });

        console.log(this.data.userRemark.split(','));

        var that = this;
        net.post(api.baseUrl + 'api/order', {
            ...app.globalData.access_token,
            'menuId': that.data.menuId,
            'orderStatus': that.data.userStatus,
            'remark': that.data.userRemark
        }).then(res => res.data).then(res => {
            console.log('order success', res);
            if (res.status === 20000) {
                showModal('提示', '成功', () => {
                    console.log(that);
                    redirectToTab('pages/all/all');
                });
            } else {
                showToast(res.message);
            }
        }).catch(e => {
            redirectToLogin();
            console.error('order fail ...', e);
        }).then(() => {
            wx.hideLoading();
        });
    },

    radioChangeRemark: function (e) {
        console.log(e.detail.value);
        this.setData({
            userRemark: e.detail.value
        });
    },
    radioChangeStatus: function (e) {
        console.log('userStatus', e.detail.value);
        this.setData({
            userStatus: e.detail.value
        });
    },


    changeAutoplay: function (e) {
        this.setData({
            autoplay: !this.data.autoplay
        })
    },
    intervalChange: function (e) {
        this.setData({
            interval: e.detail.value
        })
    },
    durationChange: function (e) {
        this.setData({
            duration: e.detail.value
        })
    }
})